package com.blb.ssh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.blb.ssh.dao.RoleDao;
import com.blb.ssh.entity.Menu;
import com.blb.ssh.entity.Role;

public class RoleDaoImpl implements RoleDao {

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	//获取数据总条数
	@SuppressWarnings("unchecked")
	public Long getRoleCount() {
		Session session =sessionFactory.getCurrentSession();
		
		String hql="select count(*) from Role";
		Query<Long> query = session.createQuery(hql);
		@SuppressWarnings("deprecation")
		long count=query.iterate().next().intValue();
		
	
		return count;
	}

	//分页查询
	@SuppressWarnings("unchecked")
	public List<Role> getRoleListByMap(int page, int rows) {
		Session session =sessionFactory.getCurrentSession();
		
		
		List<Role> ulist=new ArrayList<Role>();
		Query<Role> query = session.createQuery("from Role");
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<Role> list = query.list();
		for(Role role:list){
			ulist.add(role);
		}
		

		return ulist;
	}
	
	//条件查询
	public List<Role> getRoleListByMap(String rolename, int page, int rows){
		Session session =sessionFactory.getCurrentSession();
		
		
		List<Role> ulist=new ArrayList<Role>();
		Query<Role> query = session.createQuery("from Role where roleName=:roleName");
		query.setParameter("roleName", rolename);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<Role> list = query.list();
		for(Role role:list){
			ulist.add(role);
		}
		

		return ulist;
	}
	
	//批量删除
	public boolean batchDeleteRole(String[] idstr) {
		Session session =sessionFactory.getCurrentSession();
		
		
		boolean isok=true;
		String hql="";
		for(int i=0;i<idstr.length;i++){
			if(i ==0){
				hql="roleId='"+idstr[i]+"'";
			}else{
				hql=hql+" or roleId='"+idstr[i]+"'";
			}
		}
		Query query = session.createQuery("delete from Role where "+hql);
		query.executeUpdate();
		
		
		return isok;
	}

	//用戶新增
	public boolean addRole(Role role) {
		Session session =sessionFactory.getCurrentSession();
	
		boolean isok=true;
		Role u=new Role();
		u.setRoleName(role.getRoleName());
		session.save(u);
		
		
		return isok;
	}

	//用戶修改
	public boolean editRole(Role role) {
		Session session =sessionFactory.getCurrentSession();
		
		boolean isok=true;
		Role u=session.get(Role.class, role.getRoleId());
		u.setRoleId(role.getRoleId());
		u.setRoleName(role.getRoleName());
		session.update(u);
		
		
		return isok;
	}

	
	public Role getUserByUserName(String roleName){
		Session session =sessionFactory.getCurrentSession();
		
		
		Role r=null;
		Query<Role> query = session.createQuery("from Role where roleName=:roleName");
		query.setParameter("roleName", roleName);
		List<Role> list = query.list();
		for(Role role:list){
			r=new Role();
			r.setRoleId(role.getRoleId());
			r.setRoleName(role.getRoleName());
		}
		return r;
	}
	
	public List<Menu> getmenuId(int roleId){
		Session session =sessionFactory.getCurrentSession();
		
		
		List<Menu> mlist=new ArrayList<Menu>();
		String hql="select t2.* from t_role_menu t ,t_menu t2 where t.menu_id =t2.menu_id and t.role_id =:roleId";
		NativeQuery<Menu> nativeQuery = session.createSQLQuery(hql);
		nativeQuery.setParameter("roleId", roleId);
		for(Menu m:nativeQuery.addEntity(Menu.class).list()){
			Menu menu=new Menu();
			menu.setMenuId(m.getMenuId());
			menu.setMenuPid(m.getMenuPid());
			menu.setMenuName(m.getMenuName());
			menu.setMenuUrl(m.getMenuUrl());
			menu.setMenuType(m.getMenuType());
			menu.setMenuRole(m.getMenuRole());
			mlist.add(menu);
		}
		return mlist;
	}
	
	public List<Menu> getAllTree(){
		Session session =sessionFactory.getCurrentSession();
		
		List<Menu> mlist=new ArrayList<Menu>();
		Query<Menu> query = session.createQuery("from Menu");
		for(Menu m:query.list()){
			mlist.add(m);
		}
		return mlist;
	}
	//先通过roleId删除原来的数据
	public boolean deleteRoleMenu(int roleId){
		Session session =sessionFactory.getCurrentSession();
		boolean isok=true;
		
		try{
			NativeQuery nativeQuery = session.createSQLQuery("delete from t_role_menu where role_id=:roleId");
			nativeQuery.setParameter("roleId", roleId);
			nativeQuery.executeUpdate();
		}catch(Exception e){
			isok=false;
			e.printStackTrace();
		}
		
		return isok;
	}
	//再通过新传过来的menuId重新添加
	public boolean addRoleMenu(String[] idstr, int roleId){
		Session session =sessionFactory.getCurrentSession();
		boolean isok=true;
		try{
			String sql="";
			for(int i=0;i<idstr.length;i++){
				sql="insert into t_role_menu value(?0,?1)";
				NativeQuery nativeQuery = session.createSQLQuery(sql);
				nativeQuery.setParameter(0, roleId);
				nativeQuery.setParameter(1, idstr[i]);
				nativeQuery.executeUpdate();
			}
		}catch(Exception e){
			isok=false;
			e.printStackTrace();
		}
		
		return isok;
	}
	
	public boolean deleteRoleMenu(String[] idstr){
		Session session =sessionFactory.getCurrentSession();
		boolean isok=true;
		
		try{
			for(int i=0;i<idstr.length;i++){
				NativeQuery nativeQuery = session.createSQLQuery("delete from t_role_menu where role_id=:roleId");
				nativeQuery.setParameter("roleId", idstr[i]);
				nativeQuery.executeUpdate();
			}
		}catch(Exception e){
			isok=false;
			e.printStackTrace();
		}
		
		return isok;
	}
}

	

